package com.mindskip.xzs.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mindskip.xzs.constant.CommonRedisKey;
import com.mindskip.xzs.domain.Department;
import com.mindskip.xzs.repository.DepartmentMapper;
import com.mindskip.xzs.service.DepartmentService;
import com.mindskip.xzs.service.QywxService;
import com.mindskip.xzs.viewmodel.IdNameKV;
import com.mindskip.xzs.viewmodel.admin.dept.DeptTreeRes;
import com.mindskip.xzs.viewmodel.admin.qywx.QywxDepartmentTree;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Resource
    private QywxService qywxService;
    @Resource
    private RedisTemplate<String,List<IdNameKV>> redisTemplate;

    @Override
    public DeptTreeRes queryDeptTree() {
        QywxDepartmentTree departAndUserTree = qywxService.getDepartAndUserTree(true);
        if (departAndUserTree == null) {
            return null;
        }
        DeptTreeRes deptTreeRes = new DeptTreeRes()
                .setId(departAndUserTree.getId())
                .setLabel(departAndUserTree.getName());
        if (CollectionUtil.isNotEmpty(departAndUserTree.getChildDepartments())) {
            ArrayList<DeptTreeRes> list = new ArrayList<>();
            departAndUserTree.getChildDepartments().forEach(child -> list.add(copy(child)));
            deptTreeRes.setChildren(list);
        }
        return deptTreeRes;
    }

    @Override
    public List<IdNameKV> queryDeptKVListInCache(String keyword) {
        List<IdNameKV> deptKVResList = redisTemplate.opsForValue().get(CommonRedisKey.ALL_DEPARTMENT_KV_KEY);
        if (StringUtils.isNotBlank(keyword) && deptKVResList != null) {
            deptKVResList = deptKVResList.stream().filter(deptKVRes -> deptKVRes.getName().contains(keyword)).collect(Collectors.toList());
        }
        return deptKVResList;
    }

    @Override
    public List<IdNameKV> queryUserDeptKV(Integer userId) {
        return baseMapper.queryUserDeptKVList(userId);
    }

    /**
     * 更新缓存
     */
    @Override
    public void updateDeptsKVInCache() {
        List<IdNameKV> deptList = baseMapper.queryDeptKVList();
        if (CollectionUtil.isNotEmpty(deptList)) {
            redisTemplate.opsForValue().set(CommonRedisKey.ALL_DEPARTMENT_KV_KEY, deptList, 30, TimeUnit.DAYS);
        }
    }







    private DeptTreeRes copy(QywxDepartmentTree departAndUserTree) {
        DeptTreeRes deptTreeRes = new DeptTreeRes()
                .setId(departAndUserTree.getId())
                .setLabel(departAndUserTree.getName());
        if (CollectionUtil.isNotEmpty(departAndUserTree.getChildDepartments())) {
            ArrayList<DeptTreeRes> list = new ArrayList<>();
            departAndUserTree.getChildDepartments().forEach(child -> list.add(copy(child)));
            deptTreeRes.setChildren(list);
        }
        return deptTreeRes;
    }
}
